package cn.tedu.tmall.admin.mall.controller;

import cn.tedu.tmall.admin.mall.pojo.param.MallGoodsAddParam;
import cn.tedu.tmall.admin.mall.pojo.vo.GoodsItemListVO;
import cn.tedu.tmall.admin.mall.pojo.vo.GoodsVO;
import cn.tedu.tmall.admin.mall.service.IMallGoodsService;
import cn.tedu.tmall.common.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@Api(tags = "商城商品管理相关接口")
@RequestMapping("mall_goods")
@RestController
public class GoodsController {
    //增加商品 需要挂到类别上
    //通过类别查询商品列表
    //通过商品id查询商品详情
    // 30分钟
    @Autowired
    private IMallGoodsService goodsService;

    @ApiOperation("添加商品")
    @PostMapping("add")
    private JsonResult addGoods(MallGoodsAddParam mallGoodsAddParam) {
        goodsService.addGoods(mallGoodsAddParam);
        return JsonResult.ok();
    }


    @ApiOperation("商品列表")
    @PostMapping("/list/")
    public JsonResult listGoods(@RequestParam("categoryId") Long categoryId,
                                @RequestParam("pageNum") Integer pageNum,
                                @RequestParam("pageSize") @Range(min = 1, max = 20,message = "页数太大")Integer pageSize) {
        List<GoodsItemListVO> goodsVOS = goodsService.listGoods(categoryId, pageNum, pageSize);
        return JsonResult.ok(goodsVOS);
    }


    @ApiOperation("根据商品id查询商品详情")
    @PostMapping("detail")
    private JsonResult detailGoods(Long goodsId) {
        GoodsVO goods = goodsService.detail(goodsId);
        return JsonResult.ok(goods);
    }

}
